<template>
  <view class="container">
    <view class="bgc">
      <image src="../../static/img/loginbgc.png" mode="aspectFill" style="width: 100%; height: 650rpx"></image>
    </view>
    <view style="width: 180rpx; height: 180rpx; background-color: #fff; position: absolute; top: 300rpx; left: 290rpx">
      <image src="../../static/img/logo.png" mode="aspectFill" class="logo"></image>
      <view style="font-size: 30rpx; font-family: PingFang SC, PingFang SC; font-weight: bold; color: #222222; text-align: center; margin-top: 20rpx">考神临门</view>
    </view>
    <view
      style="
        display: flex;
        align-items: center;
        background-color: #f2f2f4;
        width: 600rpx;
        height: 90rpx;
        border-radius: 30rpx;
        margin: 0 auto;
        margin-bottom: 30rpx;
        box-sizing: border-box;
        padding: 0 30rpx;
      "
    >
      <view style="font-size: 30rpx; font-weight: blod">账号</view>
      <input v-model="account" style="margin-left: 30rpx" type="text" placeholder="请输入账号" />
    </view>
    <view
      style="
        display: flex;
        align-items: center;
        background-color: #f2f2f4;
        width: 600rpx;
        height: 90rpx;
        border-radius: 30rpx;
        margin: 0 auto;
        box-sizing: border-box;
        padding: 0 30rpx;
        margin-bottom: 50rpx;
      "
    >
      <view>密码</view>
      <input v-model="password" style="margin-left: 30rpx" type="password" placeholder="请输入密码" />
    </view>
    <button v-if="false" class="login" open-type="getPhoneNumber" @getphonenumber="fnlogin">授权登录</button>
    <!--  <view class="login" @click="fnlogin2">登录</view> -->
    <view class="login" @click="login2">登录</view>
    <view style="text-align: center; margin-top: 30rpx">
      <label class="radio">
        <radio value="checked" :checked="checked" color="#3553E8" style="transform: scale(0.7)" @click="checked = !checked" />
        <text style="font-size: 24rpx; font-family: PingFang SC, PingFang SC; font-weight: 500; color: #666666">
          我已阅读并同意
          <text @click="gofuwu">《用户服务协议》</text>
          和
          <text @click="goyinsi">《隐私协议》</text>
        </text>
      </label>
    </view>
    <!-- 测试登录 -->
    <!-- <view @click="login2">登录</view> -->
    <!-- 获取用户的昵称和头像 -->
    <uni-popup ref="popup" type="bottom">
      <view class="box">
        <!-- logo -->
        <view class="fl" style="justify-content: center">
          <image src="../../static/img/logo.png" mode="" class="logo"></image>
          <view style="font-size: 30rpx; font-weight: 800">考神临门</view>
        </view>
        <!-- 获取用户头像 -->
        <view class="fl" style="margin-top: 30rpx">
          <view>微信头像</view>
          <view>
            <button open-type="chooseAvatar" class="chooseavatar" @chooseavatar="getavatar" v-if="avatar == ''">选择头像</button>
            <image :src="imgUrl + avatar" mode="" style="width: 120rpx; height: 120rpx; margin-left: 30rpx" v-else></image>
          </view>
        </view>
        <!-- 获取昵称 -->
        <view class="fl" style="margin-top: 30rpx">
          <view style="font-size: 28rpx">微信昵称</view>
          <input type="nickname" style="box-sizing: border-box; flex: 1; padding-left: 40rpx" @change="getname" />
        </view>
        <view style="display: flex; align-items: center; justify-content: space-around; margin-top: 40rpx">
          <view class="btn1" @click="close">暂不登录</view>
          <view class="btn2" @click="goindex">登录</view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      account: '',
      password: '',
      imgUrl: '',
      checked: false,
      nickname: '',
      avatar: '',
      code: '',
      mobile_code: '',
      logincode: ''
    };
  },
  methods: {
    // 测试登录
    login2() {
      uni.$http
        .post('/user/login', {
          account: this.account,
          password: this.password
        })
        .then((res) => {
          console.log(res.data.data);
          uni.setStorageSync('userinfo', res.data.data.userinfo);
          uni.setStorageSync('token', res.data.data.userinfo.token);
          if (res.data.code == 1) {
            uni.reLaunch({
              url: '/pages/index/index?status=' + 1
            });
          }
        });
    },
    // 点击登录
    goindex() {
      if (this.nickname == '') {
        uni.showToast({
          title: '请填写昵称和头像',
          icon: 'none'
        });
        return;
      }
      if (!this.checked) {
        return uni.showToast({
          title: '请阅读协议',
          icon: 'none'
        });
      }
      uni.showLoading();
      // this.avatar = '';
      uni.login({
        success: async (res) => {
          console.log(res);
          const res2 = await uni.$http.post('/user/weChatMiniLogin', {
            code: res.code,
            mobile_code: this.mobile_code,
            avatar: this.avatar,
            nickname: this.nickname
          });
          console.log('登录的结果', res2.data);
          if (res2.data.code == 1) {
            uni.setStorageSync('userinfo', res2.data.data);
            uni.setStorageSync('token', res2.data.data.token);
            if (res2.data.data.position == 1) {
              uni.redirectTo({
                url: '/pages/index/index?status=' + 1
              });
            } else if (res2.data.data.position == 2) {
              uni.redirectTo({
                url: '/pages/index/index?status=' + 2
              });
            }
          } else {
            uni.showToast({
              title: '登录失败，请重新登录',
              icon: 'none'
            });
          }
        }
      });
      uni.hideLoading();
    },
    // 获取昵称
    getname(e) {
      console.log('昵称', e);
      this.nickname = e.detail.value;
    },
    // 获取用户的头像
    getavatar(e) {
      console.log('头像-----', e);
      if (e.detail.avatarUrl != '') {
        uni.uploadFile({
          url: this.upimg + '/api/common/upload',
          filePath: e.detail.avatarUrl,
          name: 'file',
          success: (res) => {
            console.log('上传的图片', JSON.parse(res.data));
            this.avatar = JSON.parse(res.data).data.url;
          }
        });
      }
    },
    // 测试
    fnlogin2() {
      if (!this.checked) {
        return uni.showToast({
          title: '请阅读协议',
          icon: 'none'
        });
      }
      // 测试
      uni.setStorageSync(
        'userinfo',
        '{"id":9,"username":"花花2","nickname":"🐉","mobile":"15138483174","avatar":"/uploads/20240424/bb8cb6b9dac751e51da7f6625a818fe2.jpg","score":0,"position":"2","position_status":2,"token":"acb86dc8-f5d1-4ce2-b343-2b6aaab5c2e5","user_id":9,"createtime":1736218408,"expiretime":1738810408,"expires_in":2592000,"user_code":0}'
      );
      uni.setStorageSync('token', 'acb86dc8-f5d1-4ce2-b343-2b6aaab5c2e5');
      uni.reLaunch({
        url: '/pages/index/index?status=' + 1
      });
    },
    // 点击获取手机号
    fnlogin(e) {
      console.log('手机号', e);
      if (!this.checked) {
        return uni.showToast({
          title: '请阅读协议',
          icon: 'none'
        });
      }
      // 测试

      uni.login({
        success: (res) => {
          console.log(res);
          uni.$http
            .post('/user/checkOpenid', {
              code: res.code
            })
            .then((res2) => {
              console.log(res2.data.data.user_code);
              uni.login({
                success: (res) => {
                  if (res2.data.data.user_code == 0) {
                    uni.$http
                      .post('/user/weChatMiniLogin', {
                        code: res.code,
                        mobile_code: e.detail.code,
                        avatar: '',
                        nickname: ''
                      })
                      .then((res3) => {
                        if (res3.data.code == 1) {
                          uni.setStorageSync('userinfo', res3.data.data);
                          uni.setStorageSync('token', res3.data.data.token);
                          uni.reLaunch({
                            url: '/pages/index/index?status=' + 1
                          });
                        }
                      });
                  } else if (res2.data.data.user_code == 1) {
                    this.$refs.popup.open();
                    this.mobile_code = e.detail.code;
                  }
                }
              });
            });
        }
      });
    },
    // 关闭弹框
    close() {
      this.$refs.popup.close();
      this.nickname = '';
      this.avatar = '';
    },

    // 跳转到服务协议
    gofuwu() {
      uni.navigateTo({
        url: '/pages/fwxy/fwxy'
      });
    },
    //  跳转到隐私协议
    goyinsi() {
      uni.navigateTo({
        url: '/pages/ysxy/ysxy'
      });
    }
  },
  onLoad() {
    this.imgUrl = this.imgurl;
  }
};
</script>

<style lang="scss" scoped>
.container {
  box-sizing: border-box;
  .logo {
    width: 180rpx;
    height: 180rpx;
  }
  .login {
    box-sizing: border-box;
    width: 690rpx;
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    color: #fff;
    margin: 0 30rpx;
    background: #3553e8;
    font-family: PingFang SC, PingFang SC;
    font-weight: 500;
    font-size: 30rpx;
    border-radius: 100rpx 100rpx 100rpx 100rpx;
    padding: 0 !important;
  }
  .box {
    box-sizing: border-box;

    width: 750rpx;
    height: 600rpx;
    padding: 30rpx;
    background-color: #fff;
  }
  .chooseavatar {
    box-sizing: border-box;
    background-color: transparent;
    font-size: 22rpx;
    width: 200rpx;
    padding: 0;
    margin: 0;
    text-align: left;
    margin-left: 40rpx;
  }
  .chooseavatar::after {
    border: none;
  }
  .fl {
    display: flex;
    align-items: center;
  }
  .btn1 {
    box-sizing: border-box;
    width: 300rpx;
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    font-size: 28rpx;
    background-color: #f8f8f8;
    color: #3553e8;
    border-radius: 100rpx;
  }
  .btn2 {
    box-sizing: border-box;
    width: 300rpx;
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    font-size: 28rpx;
    color: #fff;
    background-color: #3553e8;
    border-radius: 100rpx;
  }
}
</style>
